package com.nf.dao;

import com.nf.entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
@Scope("prototype")
public class UserDaoImpl implements UserDao {

    @Autowired
    private SessionFactory sessionFactory = null;

    public List<User> getAllUser() {
        List<User> userList = new ArrayList<User>();
        //spring将直接替代掉下面3行代码
        //Configuration configuration = new Configuration();
        //configuration.configure("hibernate.cfg.xml.bak");
        //factory = configuration.buildSessionFactory();
        System.out.println("SessionFactory:"+sessionFactory);
        //Session session = sessionFactory.openSession();
        //保证了同1个回话中，使用同1个session
        Session session = sessionFactory.getCurrentSession();


        //session.beginTransaction();

        /*

        Query query = session.createQuery("from User u where u.id>:myid");
        //Query query = session.createNamedQuery("selectUserById");

        /*
        Query query = session.createNamedQuery("myQuery",User.class);
        */
        /*
        query.setParameter("myid",1);
        userList = query.getResultList();

        for (User u:userList){
            System.out.println("id:"+u.getId());
        }

        session.close();
        //当spring注入sessionFactory时，它为单例
        //不能随便关闭它，否则下个请求不能使用

        //sessionFactory.close();

        System.out.println(userList+" 大小："+userList.size());
        */
        /*
        Transaction transaction = session.beginTransaction();
        try{
            //第一个操作，删除
            User user1 = session.get(User.class,8);
            System.out.println("user1"+user1.getName());
            session.delete(user1);
            System.out.println("已经删除");
            //第2个操作，新增

            User user3 = new User();
            user3.setName("小小");
            user3.setSex("123");
            session.save(user3);

            User user2 = new User();
            user2.setName("小红");
            user2.setSex("12");
            session.save(user2);
            transaction.commit();
        }catch (Exception e){
            System.out.println("错误："+e.getMessage());
            transaction.rollback();
        }
        */
//第一个操作，删除
        User user1 = session.get(User.class,8);
        //System.out.println("user1"+user1.getName());
        if (user1!=null) {
            session.delete(user1);
            System.out.println("已经删除");
        }
        //第2个操作，新增

        User user2 = new User();
        user2.setName("小红");
        user2.setSex("12");
        session.save(user2);

        User user3 = new User();
        user3.setName("小小");
        user3.setSex("1238888888888");
        session.save(user3);







        return userList;
    }
}
